library(tidyverse)
library(cowplot)
library(pdftools)
load(here::here('data/data.RData'))

data %>%
  mutate(
    SO = case_when(
      SO == "Austral: Brazilian Journal of Strategy and International Relations" ~ "Austral",
      SO == "Brazilian Journal of International Relations" ~ "BJIR",
      SO == "Conjuntura Austral: Journal of the Global South" ~ "Conjuntura Austral",
      SO == "Estudos Internacionais: Revista de Relações Internacionais da PUC Minas" ~"Estudos Internacionais",
      SO == "Meridiano 47 - Journal of Global Studies" ~ "Meridiano 47",
      SO == "Monções: Revista de Relações Internacionais da UFGD" ~ "Monções",
      SO == "Oikos - Revista de Economia Politica Internacional" ~ "Oikos",
      SO == "Revista Brasileira de Estudos de Defesa" ~ "RBED",
      SO == "Revista Brasileira de Politica Internacional" ~ "RBPI",
      TRUE ~ SO
      
    )
  ) %>% 
  filter(PY < 2022) -> data

data %>%
  separate_rows(GND, sep = ';') %>%
  mutate(GND = str_trim(GND)) %>%
  distinct() %>%
  filter(GND != "NA") -> data_GND


data_GND %>%
  group_by(PY,GND) %>%
  count() %>%
  ungroup() %>%
  group_by(PY) %>%
  mutate(prop = round(n  / sum(n),2) * 100) %>%
  ungroup() -> data_plot9c

data_plot9c %>%
  filter(PY %in% c(1997,2021)) -> data_geom_point_plot9c

data_geom_point_plot9c %>%
  filter(PY == 2021) -> label_plot9c

data_plot9c %>%
  ggplot(aes(x = PY,
             y = prop,
             col = GND)) +
  geom_line(lwd = 1) +
  geom_point(
    data = data_geom_point_plot9c,
    aes(x = PY, y = prop, col = GND),
    size = 3
  ) +
  geom_text(
    data = data_geom_point_plot9c,
    aes(x = PY, y = prop, col = GND, label = glue::glue('{prop}%')),
    fontface = "bold",
    vjust = if_else(data_geom_point_plot9c$GND == "Female",1.8,-1)
  ) +
  geom_text(
    data = label_plot9c,
    aes(x = PY, y = prop, col = GND, label = GND),
    fontface = "bold",
    vjust = if_else(label_plot9c$GND == "Female",-1,2)
  ) +
  scale_color_manual(values = c('#e34a27','#2384C0')) +
  scale_x_continuous(breaks = seq(1997,2021,2)) +
  labs(y = "",x = "YEAR") +
  scale_y_continuous(limits = c(0,100), breaks = c(0,50,100)) +
  theme_minimal() +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(20,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    ),
    legend.position = "none",
    axis.line.x = element_line(color = '#444444', size = 1.2)
  ) -> plot9c


# data_plot9c %>%
#   mutate(GND = as_factor(GND) %>%  fct_relevel(.,"Male","Female")) %>% 
#   ggplot() +
#   geom_bar(
#     aes(x = PY, y = prop, fill = GND),
#     position = 'fill',
#     stat = 'identity'
#   ) +
#   scale_x_continuous(breaks = seq(1997,2021,2)) +
#   scale_y_continuous(breaks = seq(0,1,.5),labels = scales::percent) +
#   scale_fill_manual(values = c('#2384C0','#e34a27'))+
#   theme_minimal() +
#   theme(
#     axis.text = element_text(color = '#444444'),
#     plot.margin = margin(10,10,10,10),
#     panel.grid.minor = element_blank(),
#     panel.grid.major = element_blank(),
#     panel.grid.major.y = element_line(color = "#44444430"),
#     axis.title.x = element_text(
#       margin = margin(t = 10),
#       size = 7
#     ),
#     axis.title.y = element_text(
#       margin = margin(r = 10)
#     ),
#     axis.line.x = element_line(
#       color = '#444444',
#       size = 1.2
#     ),
#     axis.text.x = element_text(margin = margin(t = 6)),
#     legend.position = "top",
#     legend.direction = 'horizontal',
#     legend.title = element_blank()
#   )


ggsave(
  plot = plot9c,
  filename = here::here('outputs/figure-06.pdf'),
  device = cairo_pdf,
  dpi = 400
)

pdf_convert(
  pdf = here::here('outputs/figure-06.pdf'),
  filenames = here::here('outputs/figure-06.jpeg'),
  format = "jpeg", dpi = 400
)
